package org.hubwork.base.common;

import java.lang.reflect.Method;
import org.springframework.aop.MethodMatcher;

public class WrappedMethodMatcher implements MethodMatcher{

	private final MethodMatcher delegate;

	public WrappedMethodMatcher(MethodMatcher delegate){

		super();
		this.delegate = delegate;
	}

	public boolean isRuntime(){
		return delegate.isRuntime();
	}

	@SuppressWarnings("rawtypes")
	public boolean matches(Method arg0, Class arg1, Object[] arg2){
		return delegate.matches(arg0, arg1, arg2);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean matches(Method arg0, Class arg1){

		try{
			Class base = arg1.getSuperclass();
			Method m = base.getMethod(arg0.getName(), arg0.getParameterTypes());
			if (delegate.matches(m, base)){
				return true;
			}
		}
		catch (Throwable t){
		}

		if (delegate.matches(arg0, arg1)){
			return true;
		}else{
			return false;
		}
	}
}